home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Process View / ProcessCheckerSettings.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  4.7 KB  |  154 lines

  1. /*******************************************************************************
  2.   ProcessChecker Demo
  3.   Example submitted by David Clegg
  4.  
  5.   This unit contains the ProcessCheckerSettings class, which is used to store
  6.   the settings for the application.
  7. *******************************************************************************/
  8. using System;
  9. using System.IO;
  10. using System.Runtime.Serialization;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. using System.Collections;
  13. using System.Windows.Forms;
  14.  
  15. namespace ProcessChecker
  16. {
  17.  
  18.     /// <summary>
  19.     /// Class to persist the settings for ProcessChecker. It is marked with the
  20.     /// [Serializable] attribute so we can serialize it to disk. All other
  21.     /// classes exposed as properties of this class must also be marked as
  22.     /// [Serializable], or have their field declarations marked as
  23.     /// [NonSerialized].
  24.     /// </summary>
  25.     [Serializable]
  26.     public class ProcessCheckerSettings {
  27.         private const int CHECK_FREQUENCY = 5;
  28.         private const bool RESTART_ONE_PROCESS = true;
  29.         private const string SETTINGS_EXTENSION = "dat";
  30.  
  31.         private ProcessList fWatchedProcesses;
  32.         private int fCheckFrequency;
  33.         private bool fRestartOneProcess;
  34.         private bool fEnabled;
  35.  
  36.         //I don't want the event handlers to be serialized, but the NonSerialized
  37.         //attribute can only be applied to field declarations. Hence the
  38.         //'non-standard' event declaration.
  39.         [NonSerialized]
  40.         private EventHandler fSettingsLoaded;
  41.         [NonSerialized]
  42.         private EventHandler fSettingsSaved;
  43.  
  44.         public event EventHandler SettingsLoaded {
  45.             add {fSettingsLoaded += value;}
  46.             remove {fSettingsLoaded -= value;}
  47.         }
  48.  
  49.         public event EventHandler SettingsSaved {
  50.             add {fSettingsSaved += value;}
  51.             remove {fSettingsSaved -= value;}
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Container to hold details of all processes to be watched.
  56.         /// </summary>
  57.         public ProcessList WatchedProcesses {
  58.             get {return fWatchedProcesses;}
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Frequency, in seconds, that we should check to see if any
  63.         /// processes need to be restarted.
  64.         /// </summary>
  65.         public int CheckFrequency {
  66.             get {return fCheckFrequency;}
  67.             set {fCheckFrequency = value;}
  68.         }
  69.  
  70.         /// <summary>
  71.         /// If this is true, only one process will be restarted every time we
  72.         /// check the watched processes.
  73.         /// </summary>
  74.         public bool RestartOneProcess {
  75.             get {return fRestartOneProcess;}
  76.             set {fRestartOneProcess = value;}
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Indicates whether process checking is enabled or not.
  81.         /// </summary>
  82.         public bool Enabled {
  83.             get {return fEnabled;}
  84.             set {fEnabled = value;}
  85.         }
  86.  
  87.         /// <summary>
  88.         /// ProcessCheckerSettings constructor.
  89.         /// </summary>
  90.         public ProcessCheckerSettings() {
  91.             fWatchedProcesses = new ProcessList();
  92.             InitDefaults();
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Serialize a ProcessCheckerSettings instance from disk.
  97.         /// </summary>
  98.         public static ProcessCheckerSettings Load() {
  99.             ProcessCheckerSettings pcs;
  100.             //The ProcessCheckerSettings will be deserialized from .\ProcessChecker.dat
  101.             string fileName = Path.ChangeExtension(Application.ExecutablePath, SETTINGS_EXTENSION);
  102.             if (File.Exists(fileName)) {
  103.                 //Create the FileStream to stream in the .dat file, and
  104.                 //deserialize using the BinaryFormatter class.
  105.                 FileStream fs = new FileStream(fileName, FileMode.Open);
  106.                 try {
  107.                     BinaryFormatter bf = new BinaryFormatter();
  108.                     pcs = (ProcessCheckerSettings)bf.Deserialize(fs);
  109.                 }
  110.                 finally {
  111.                     fs.Close();
  112.                 }
  113.             }
  114.             else {
  115.                 //.dat file not found so create a new instance and return that.
  116.                 pcs = new ProcessCheckerSettings();
  117.             }
  118.             //Fire the event handler(s) if any delegates have been assigned.
  119.             if (pcs.fSettingsLoaded != null)
  120.                 pcs.fSettingsLoaded(pcs, null);
  121.             return pcs;
  122.         }
  123.  
  124.         /// <summary>
  125.         /// Serialize the ProcessCheckerSettings instance to disk.
  126.         /// </summary>
  127.         public void Save() {
  128.             //The instance will be serialized to .\ProcessChecker.dat
  129.             string fileName = Path.ChangeExtension(Application.ExecutablePath, SETTINGS_EXTENSION);
  130.             FileStream fs = new FileStream(fileName, FileMode.Create);
  131.             try {
  132.                 //Create the FileStream to stream out the .dat file, and
  133.                 //serialize using the BinaryFormatter class.
  134.                 BinaryFormatter bf = new BinaryFormatter();
  135.                 bf.Serialize(fs, this);
  136.             }
  137.             finally {
  138.                 fs.Close();
  139.             }
  140.             //Fire the event handler(s) if any delegates have been assigned.
  141.             if (fSettingsSaved != null)
  142.                 fSettingsSaved(this, null);
  143.         }
  144.  
  145.         /// <summary>
  146.         /// Initialize default values for a new ProcessCheckerSettings instance.
  147.         /// </summary>
  148.         private void InitDefaults() {
  149.             fCheckFrequency = CHECK_FREQUENCY;
  150.             fRestartOneProcess = RESTART_ONE_PROCESS;
  151.         }
  152.     }
  153. }
  154.